class BaomingsController < ApplicationController
  # GET /baomings
  # GET /baomings.xml
  layout "admin"
  before_filter :admin? ,:except=>[:show,:new,:create]
  def index
    @baomings = Baoming.paginate(:page=>params[:page],:per_page=>15,:include=>"tuangou_pro")
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @baomings }
    end
  end

  # GET /baomings/1
  # GET /baomings/1.xml
  def show
    @baoming = Baoming.find(params[:id])
    
    respond_to do |format|
      format.html {render :layout=>"front_page"}
      format.xml  { render :xml => @baoming }
    end
  end

  # GET /baomings/new
  # GET /baomings/new.xml
  def new
    @baoming = Baoming.new
    @product=Product.find_by_sql(%{select a.title, b.id,b.name,d.display_name from tuangou_pros a
inner join products b on a.product_id=b.id 
left join
product_attribute_values c on (a.product_id=c.product_id and c.product_attribute_template_id=1)
left join
list_items d on d.id=c.value_int
where a.id=#{params[:tuangou_pro_id]}})
    
    @page_title="团购产品在线报名"
    @page_nav="><a href='/procates/all/products/list'>产品中心</a>>"
    @page_nav+="<a href='#{list_tuangou_pros_path}'>团购产品</a>>"
    @page_nav+="<a href='#{tuangou_pro_path(params[:tuangou_pro_id])}'>#{@product[0].title}</a>>&nbsp;"
    @page_nav+="在线报名"
    respond_to do |format|
      format.html {render :layout=>"front_page"}
      format.xml  { render :xml => @baoming }
    end
  end

  # GET /baomings/1/edit
  def edit
    @baoming = Baoming.find(params[:id])
  end

  # POST /baomings
  # POST /baomings.xml
  def create
    @tuangou_pro=TuangouPro.find(params[:tuangou_pro_id])

    @baoming=@tuangou_pro.baomings.new(params[:baoming])
  
    
    respond_to do |format|
      if @baoming.save
        #@tuangou_pro.baomings.create!(params[:baoming])
        flash[:notice] = '报名成功！'
        format.html { redirect_to(list_tuangou_pros_path) }
        format.xml  { render :xml => @baoming, :status => :created, :location => @baoming }
      else
        @product=Product.find_by_sql(%{select a.title,b.id,b.name,d.display_name from tuangou_pros a
inner join products b on a.product_id=b.id
left join
product_attribute_values c on (a.product_id=c.product_id and c.product_attribute_template_id=1)
left join
list_items d on d.id=c.value_int
where a.id=#{params[:tuangou_pro_id]}})

        @page_title="团购产品在线报名"
        @page_nav="><a href='/procates/all/products/list'>产品中心</a>>"
        @page_nav+="<a href='#{list_tuangou_pros_path}'>团购产品</a>>"
        @page_nav+="<a href='#{tuangou_pro_path(params[:tuangou_pro_id])}'>#{@product[0].title}</a>>&nbsp;"
        @page_nav+="在线报名"
        
        flash[:notice] = '报名出错.'
        format.html { render :action => "new",:layout=>"front_page" }
        format.xml  { render :xml => @baoming.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /baomings/1
  # PUT /baomings/1.xml
  def update
    @baoming = Baoming.find(params[:id])

    respond_to do |format|
      if @baoming.update_attributes(params[:baoming])
        flash[:notice] = 'Baoming was successfully updated.'
        format.html { redirect_to(baomings_path) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @baoming.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /baomings/1
  # DELETE /baomings/1.xml
  def destroy
    @baoming = Baoming.find(params[:id])
    @baoming.destroy

    respond_to do |format|
      format.html { redirect_to(baomings_url) }
      format.xml  { head :ok }
    end
  end
end
